#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;

int main()
{
	ll n, m;

	while (scanf("%lld %lld", &n, &m) != EOF)
	{
		if (m == 1) //为1时一定是最后一个最后出来
		{
			printf("%lld\n", n);
		}

		else
		{
			ll ans = 0, i = 2;
			while (i <= n)
			{
				if (ans + m < i)
				{
					ll tmp = (i - ans - 1) % (m - 1) ? (i - ans - 1) / (m - 1) : (i - ans - 1) / (m - 1) - 1;
					if (i + tmp > n)
					{
						ans = ans + (n + 1 - i) * m;
						break;
					}
					i += tmp;
					ans = ans + tmp * m;
				}
				else
				{
					ans = (ans + m) % i;
					i++;
				}
			}
			ans = (ans + 1) % (n + 1); //防止n%n=0

			printf("%lld\n", ans);
		}
	}
	return 0;
}



//递归写法
int f(int n, int k)
{
	if (n == 1)return 0;
	if (k == 1)return n - 1;
	if (k > n)return (f(n - 1, k) + k) % n;

	int res = f(n - n / k, k) - n % k;
	if (res < 0)res += n;
	else res += res / (k - 1);

	return res;
}

void solve()
{
	int n, m;
	while (cin >> n >> m)cout << f(n, m) + 1 << '\n';
}
